package com.ss.android.f.b.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.ss.android.f.b.a.a.AccountCacheHelper;
import com.ss.android.f.b.a.a.c;
import com.ss.android.f.b.a.a.DirectoryCacheHelper;
import com.ss.android.f.b.a.a.SharePreferenceCacheHandler;

import java.io.File;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.channels.FileLock;
import java.security.SecureRandom;
import java.util.UUID;

import inline.PackageUtils;

public class aa {
    public final AccountCacheHelper a;
    private final Context b;
    private c c;
    private static String d;
    private static String e;
    private static String f;
    private static String g;
    private static String[] h;
    private static String i;
    private static volatile aa j;

    private aa(Context arg6) {
        DirectoryCacheHelper v0_1;
        c v1;

        this.b = arg6.getApplicationContext();
        this.c = new SharePreferenceCacheHandler(this.b);
        this.a = new AccountCacheHelper(this.b);
        this.c.d = this.a;
        AccountCacheHelper v2 = this.a;
        try {
            v1 = new DirectoryCacheHelper(this.b, this.b.getExternalCacheDir().getParent() + "/bytedance");
            ((c) v2).d = ((c) v1);
        } catch (Exception v0) {
            v0.printStackTrace();
            v1 = v2;
        }

        try {
            v0_1 = new DirectoryCacheHelper(this.b, Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.snssdk.api/bytedance");
            ((c) v1).d = ((c) v0_1);
        } catch (Exception v0) {
            v0.printStackTrace();
            v0_1 = ((DirectoryCacheHelper) v1);
        }

        try {
            ((c) v0_1).d = new DirectoryCacheHelper(this.b, Environment.getExternalStorageDirectory().getPath() + "/bytedance");
        } catch (Exception v0) {
            v0.printStackTrace();
        }
    }

    public final String a() {
        String v2;
        SharedPreferences v3;
        String v0;
        if (!android.support.design.a.isTextEmpty(d)) {
            v0 = d;
            return v0;
        }

        try {
            String v1 = PackageUtils.g(this.b);
            if (v1 != null && !v1.equals("9774d56d682e549c") && v1.length() >= 13) {
                v0 = this.c.b(null, v1);
            } else {
                v3 = this.b.getSharedPreferences("snssdk_openudid", 0);
                v0 = v3.getString("openudid", null);
                if (android.support.design.a.z(v0)) {
                    this.a.b(v0, null);
                } else {
                    v2 = new BigInteger(80, new SecureRandom()).toString(16);
                    if (v2.charAt(0) == 45) {
                        v2 = v2.substring(1);
                    }

                    int v0_2 = 13 - v2.length();
                    if (v0_2 > 0) {
                        StringBuilder v4 = new StringBuilder();
                        while (v0_2 > 0) {
                            v4.append('F');
                            --v0_2;
                        }
                        v4.append(v2);
                        v2 = v4.toString();
                    }

                    v0 = this.a.b(a("openudid.dat"), v2);
                    if (!android.support.design.a.z(v0)) {
                        v0 = v2;
                    }
                    SharedPreferences.Editor v2_1 = v3.edit();
                    v2_1.putString("openudid", v0);
                    v2_1.commit();
                }
            }
        } catch (Exception v0_1) {
            Logger.w("DeviceParamsProvider", "exception when making openudid: " + v0_1);
            v0 = "";
        }

        if (!android.support.design.a.isTextEmpty(v0)) {
            d = v0;
        }
        return v0;
    }

    public static aa a(Context arg3) throws IllegalArgumentException {
        if (aa.j == null) {
            Class v1 = aa.class;
            synchronized (v1) {
                if (aa.j == null) {
                    if (arg3 == null) {
                        throw new IllegalArgumentException("context can\'t be null");
                    } else {
                        aa.j = new aa(arg3);
                    }
                }
            }
        }
        return j;
    }

    private static String a(String arg8) {
        FileLock v3_1 = null;
        Throwable v0_1;
        RandomAccessFile v2_1 = null;
        String v1_2 = null;
        int v6 = 161;
        String v0 = null;
        if (!"mounted".equals(Environment.getExternalStorageState())) {
            return v0;
        }

        try {
            v1_2 = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.snssdk.api/cache";
            String v2 = v1_2 + "/" + arg8;
            File v3 = new File(v1_2);
            if (!v3.exists() && !v3.mkdirs()) {
                return v0;
            }

            File v1_3 = new File(v2);
            v2_1 = new RandomAccessFile(v1_3, "rwd");
            v3_1 = v2_1.getChannel().lock();
            if (v1_3.isFile()) {
                byte[] v4 = new byte[161];
                int v5 = v2_1.read(v4, 0, 161);
                if (v5 > 0 && v5 < v6) {
                    v1_2 = new String(v4, 0, v5, "UTF-8");
                    if (android.support.design.a.z(v1_2)) {
                        return v1_2;
                    }
                }
            }
            v1_2 = null;
            if (!android.support.design.a.isTextEmpty(v1_2)) {
                v1_2 = null;
                byte[] v1_4 = v1_2.getBytes("UTF-8");
                v2_1.setLength(0);
                v2_1.write(v1_4);
                return v0;
            }
        } catch (Exception v1_1) {
            Logger.d("DeviceParamsProvider", "load openudid exception " + v1_1);
        } finally {
            if (v3_1 != null) {
                try {
                    v3_1.release();
                } catch (Exception v0_2) {

                }
            }
            if (v2_1 != null) {
                try {
                    v2_1.close();
                } catch (Exception v0_2) {
                }
            }
        }
        return v1_2;
    }

    public final String a(SharedPreferences arg5) {
        String v1_1;
        String v0 = null;
        if (arg5 == null && !TextUtils.isEmpty(g)) {
            return g;
        }

        if (arg5 != null) {
            v1_1 = arg5.getString("device_id", "");
        } else {
            v1_1 = v0;
        }

        try {
            if (!android.support.design.a.isTextEmpty(v1_1) && (android.support.design.a.isTextEquals(v1_1, g))) {
                return g;
            }
            v1_1 = this.c.f(v1_1, null);
            g = v1_1;
            v0 = v1_1;
        } catch (Exception v1) {
            v1.printStackTrace();
        }

        return v0;
    }

    public final String b() {
        String v1;
        SharedPreferences v2;
        String v0;
        if (!TextUtils.isEmpty(e)) {
            v0 = e;
            return v0;
        }

        try {
            v2 = this.b.getSharedPreferences("snssdk_openudid", 0);
            v0 = v2.getString("clientudid", null);
            if (!android.support.design.a.z(v0)) {
                v1 = UUID.randomUUID().toString();
                v0 = this.a.c(a("clientudid.dat"), v1);
                if (!android.support.design.a.z(v0)) {
                    v0 = v1;
                }

                SharedPreferences.Editor v1_1 = v2.edit();
                v1_1.putString("clientudid", v0);
                v1_1.commit();
                e = v0;
            } else {
                this.a.c(v0, null);
                e = v0;
            }
        } catch (Exception v0_1) {
            Logger.w("DeviceParamsProvider", "exception when making client_udid: " + v0_1);
            v0_1.printStackTrace();
            v0 = "";
        }

        return v0;
    }

    public final String c() {
        String v0;
        String v1 = null;
        if (!android.support.design.a.isTextEmpty(i)) {
            v0 = i;
            return v0;
        }

        try {
            v0 = this.c.d(null, Build.SERIAL);
            i = v0;
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0 = v1;
        }

        return v0;
    }

    public final String[] d() {
        String[] v0;
        String[] v1 = null;
        if (h != null && h.length > 0) {
            v0 = h;
            return v0;
        }

        try {
            v0 = this.c.a(null, PackageUtils.f(this.b));
            if (v0 == null) {
                v0 = new String[0];
            }

            h = v0;
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0 = v1;
        }

        return v0;
    }

    public final String e() {
        String v0;
        String v1 = null;
        if (!android.support.design.a.isTextEmpty(f)) {
            v0 = f;
            return v0;
        }

        try {
            v0 = this.c.e(null, PackageUtils.h(this.b));
            f = v0;
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            v0 = v1;
        }

        return v0;
    }
}

